<template>
  <div class="user-management">
    <!-- 添加贫困户按钮和搜索区域 -->
    <div class="search-area">
      <!-- 修改按钮点击事件 -->
      <el-button type="primary" @click="navigateToInterviewForm">+ 添加走访信息</el-button>
      <div class="search-inputs">
        <el-input v-model="searchParams.belongArea" placeholder="请输入所属区域" clearable
          style="width: 200px; margin-right: 10px;" />
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </div>
    </div>

    <!-- 贫困户表格 -->
    <div class="table-container">
      <el-table :data="pagedVisitInfo" border style="width: 100%; margin-top: 20px;">
        <el-table-column prop="visitId" label="走访编号" width="100" />
        <el-table-column prop="familyNumber" label="贫困户编号" width="90" />
        <el-table-column prop="cadreNumber" label="驻村干部编号" width="100" />
        <el-table-column prop="belongArea" label="所属地区" width="100" />
        <el-table-column prop="riskType" label="风险类型" width="120" />
        <el-table-column prop="riskRemark" label="风险备注" width="180" />
        <el-table-column prop="income" label="人均纯收入" width="90"/>
        <el-table-column prop="debt" label="家庭负债" width="90"/> 
        <el-table-column prop="suggestions" label="建议与意见" width="100"/>
        <el-table-column prop="visitContent" label="走访内容" width="200"/> 
        <el-table-column prop="visitTime" label="走访时间" width="90"> 
        </el-table-column>

       <el-table-column label="操作" width="100">
          <template #default="{ row }">
            <el-button type="text" @click="showEditDialog(row)">修改</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <!-- 分页组件 -->
    <div class="pagination-container">
      <el-pagination>
        v-model="currentPage"
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="totalVisitInfo"
        @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
    </div>
    
    <!-- 编辑对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px" :before-close="handleCloseDialog">
      <el-form :model="userForm" label-width="100px">
        <el-form-item label="走访编号" required>
          <el-input v-model="userForm.visitId" disabled/>
        </el-form-item>
        <el-form-item label="贫困户编号" required>
          <el-input v-model="userForm.familyNumber" disabled/>
        </el-form-item>
        <el-form-item label="驻村干部编号" required>
          <el-input v-model="userForm.cadreNumber" disabled/>
        </el-form-item>
        <el-form-item label="所属地区" required>
          <el-input v-model="userForm.belongArea" disabled/>
        </el-form-item>
        <el-form-item label="风险类型" required>
          <el-input v-model="userForm.riskType" />
        </el-form-item>
        <el-form-item label="风险备注" required>
          <el-input v-model="userForm.riskRemark" />
        </el-form-item>
        <el-form-item label="人均纯收入" required>
          <el-input v-model="userForm.income" />
        </el-form-item>
        <el-form-item label="家庭负债" required>
          <el-input v-model="userForm.debt" />
        </el-form-item>
        <el-form-item label="建议与意见" required>
          <el-input v-model="userForm.suggestions" />
        </el-form-item>
        <el-form-item label="走访内容" required>
          <el-input v-model="userForm.visitContent" />
        </el-form-item>
        <el-form-item label="走访时间" required>
          <el-input v-model="userForm.visitTime" disabled/>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="handleCloseDialog">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </template>
    </el-dialog>
   </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router' // 引入路由钩子
import { updateVisitInfo,getVisitInfo } from '@/api/visitinfo'

    const router = useRouter() // 获取路由实例
    // 分页参数
    const currentPage = ref(1)
    const pageSize = ref(10)

    //表格需要展示的数据对象数组
    const pagedVisitInfo=ref([])

    // 搜索参数
    const searchParams = reactive({
      currentPage:currentPage.value,
      pageSize:pageSize.value,
      belongArea:''
    })

    //分页的总记录数
    const totalVisitInfo=ref(0)
    // 查询当前表格中的数据
    const queryVisitInfo = () => {
  getVisitInfo(searchParams).then((res) => {
  console.log(`当前参数：`,searchParams)
    if (res.code == 2000) {
      pagedVisitInfo.value = res.data
      totalVisitInfo.value = res.total
    } else {
      pagedVisitInfo.value = []
      totalVisitInfo.value = 0
    }
  })
}

queryVisitInfo()

    // 对话框相关状态
    const dialogVisible = ref(false)
    const isEditing = ref(false)
    const dialogTitle = '修改走访信息'
    
    // 表单数据
    const userForm = reactive({
      visitId: '',
      familyNumber: '',
      cadreNumber: '',
      belongArea:'',
      riskType: '',
      riskRemark: '',
      income: '',
      debt: '',
      suggestions: '',
      visitContent: '',
      visitTime: ''
    })

    //显示编辑对话框
    const showEditDialog = (row) => {
      isEditing.value = true
      Object.assign(userForm, JSON.parse(JSON.stringify(row)))
      dialogVisible.value = true
  }

  // 重置表单
    const resetForm = () => {
      Object.assign(userForm, {
        visitId: '',
        familyNumber: '',
        cadreNumber: '',
        belongArea:'',
        riskType: '',
        riskRemark: '',
        income: '',
        debt: '',
        suggestions: '',
        visitContent: '',
        visitTime: ''
      })
    }

    // 关闭对话框
    const handleCloseDialog = () => {
      dialogVisible.value = false
      resetForm()
    }


    // 跳转到走访信息表单页面
    const navigateToInterviewForm = () => {
      router.push('/interview')
    }
    
    // 处理搜索
    const handleSearch = () => {
      queryVisitInfo()
    }

    const handleConfirm=()=>{
      if(!userForm.riskRemark){
        ElMessage.warning('请填写风险备注')
        return
      }
      updateVisitInfo(userForm).then(res=>{
        if(res.code==2000){
          ElMessage.success('修改成功')
          handleSearch()
        }
      })
      dialogVisible.value = false
    }

    // 处理分页大小变化
    const handleSizeChange = (val) => {
      pageSize.value = val
      queryVisitInfo()
    }

    // 处理当前页变化
    const handleCurrentChange = (val) => {
      currentPage.value = val
      queryVisitInfo()
    }

    
</script>

<style scoped>

.dialog-footer{
  justify-content: center;
  align-items: center;
  gap: 10px;
}

.el-table__header .cell {
  text-align: center !important;
}
.user-management {
  padding: 20px;
  min-height: 100vh; 
  overflow-y: auto; 
  display: flex;
  flex-direction: column;
  align-items: center;
}

.search-area {
  display: flex;
  justify-content: center; /* 水平居中 */
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 20px;
  max-width: 1250px;
}

.search-inputs {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

/* 确保按钮文字居中 */
.el-button {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}

/* 让表格中所有单元格的文字居中 */
.el-table .cell {
  text-align: center !important;
}

/* 操作按钮样式 */
.action-buttons {
  display: flex;
  justify-content: center;
  gap: 10px;
  align-items: center;
  text-align: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  width: 1250px;
  max-width: 1250px;
}

.table-container {
  width: 100%;
  max-width: 1250px;
  display: flex;
  justify-content: center;
}

.el-table {
  min-width: 100%;
  table-layout: auto;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .search-inputs {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .el-table {
    font-size: 14px;
  }
}
</style>